package com.vt.admin.controller;

import com.vt.admin.api.dto.user.UserInfo;
import com.vt.admin.service.SysSocialDetailsService;
import com.vt.admin.service.SysUserService;
import com.vt.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Auther: panZha
 * @Date: 2018/11/20 14:25
 * @Description:
 */
@RestController
@RequestMapping("/social")
@AllArgsConstructor
public class SysSocialDetailsController {

    private final SysSocialDetailsService sysSocialDetailsService;
    private final SysUserService sysUserService;

    /**
     * 通过社交账号查询用户、角色信息
     *
     * @param inStr appid@code
     * @return
     */
    @GetMapping("/info/{inStr}")
    public R<UserInfo> social(@PathVariable String inStr) {
        Map<String, String> result = sysSocialDetailsService.findOpenId(inStr);
        return new R(sysUserService.findUserInfo(result.get("type"), result.get("openId")));
    }

    /**
     * 绑定社交账号
     *
     * @return
     */
    @GetMapping("/bind")
    public R<Boolean> bind(@RequestParam String client, @RequestParam String code) {
        return new R(sysSocialDetailsService.bindSocial(client, code));
    }

    /**
     * 解除社交绑定
     *
     * @return
     */
    @GetMapping("/unbind")
    public R<Boolean> unbind() {
        return new R(sysSocialDetailsService.unbindSocial());
    }
}
